package job2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class RelationshipDeleteDemo {
    Configuration conf;
    Connection conn;
    @Before
    public void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "node1,node2");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conn = ConnectionFactory.createConnection(conf);
    }
//   需要挂载协处理器
//  alter 'relationship',METHOD => 'table_att','Coprocessor'=>'hdfs://node1:8020/processor/processor.jar|job2.MyProcessor|1001|'
    @Test
    public void deleteData() throws IOException {
//        例如删除用户1和用户2的好友关系
        String uid = "uid1";
        String fid = "uid2";
//        获取用户关系表
        Table relationship = conn.getTable(TableName.valueOf("relationship"));
//        uid为rowKey
        Delete delete = new Delete(Bytes.toBytes(uid));
//        删除列族friends下的fid好友
        delete.addColumn(Bytes.toBytes("friends"),Bytes.toBytes(fid));
//        执行删除操作
        relationship.delete(delete);
        System.out.println("删除数据成功");
//        释放表资源
        relationship.close();
    }

    @After
    public void destroy() throws IOException {
        if (conn != null) conn.close();
    }
}
